

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.logging.meta &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../index.html" />
    <link rel="up" title="pyamp.logging" href="../logging.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../logging.html" accesskey="U">pyamp.logging</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.logging.meta</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The meta module contains the LoggerMeta, and FunctionLoggerMeta classes.</span>

<span class="sd">These classes provide the ability for create a class which will have the</span>
<span class="sd">ability to be logged, or to wrap certain class functions with logging messages</span>
<span class="sd">to indicate when the function was entered and exited.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">pyamp.logging.prefix</span> <span class="kn">import</span> <span class="n">Prefix</span>
<span class="kn">from</span> <span class="nn">pyamp.logging.logger</span> <span class="kn">import</span> <span class="n">Logger</span><span class="p">,</span> <span class="n">LogLevel</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;LoggerMeta&quot;</span><span class="p">,</span> <span class="s">&quot;FunctionLoggerMeta&quot;</span><span class="p">,</span> <span class="s">&quot;doNotLog&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="LoggerMeta"><a class="viewcode-back" href="../../../_generated/pyamp_logging_meta.html#pyamp.logging.meta.LoggerMeta">[docs]</a><span class="k">class</span> <span class="nc">LoggerMeta</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The LoggerMeta is a metaclass that provides the ability to create a</span>
<span class="sd">    :class:`Logger` for a given class. The class has the ability to set the</span>
<span class="sd">    following static class properties.</span>

<span class="sd">    * *logLevel* -- Set log level for the logger</span>
<span class="sd">    * *logPrefix* -- Set the log prefix for the logger</span>

<span class="sd">    Any classes that use LoggerMeta as their metaclass will have a &#39;logger&#39;</span>
<span class="sd">    property created, which will be a :class:`Logger` object with the preset</span>
<span class="sd">    logLevel, and logPrefix.</span>

<span class="sd">    Example::</span>

<span class="sd">        class Example:</span>
<span class="sd">            __metaclass__ = LoggerMeta</span>
<span class="sd">            logLevel = LogLevel.INFO</span>
<span class="sd">            logPrefix = Prefix(text=&quot;TestPrefix&quot;)</span>

<span class="sd">        if __name__ == &#39;__main__&#39;:</span>
<span class="sd">            example = Example()</span>

<span class="sd">            # Prints: [Example]: [TestPrefix]: This is an information message</span>
<span class="sd">            example.logger.info(&quot;This is an information message&quot;)</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="c"># Define the static properties name as used by the main class</span>
    <span class="n">LogLevel</span> <span class="o">=</span> <span class="s">&quot;logLevel&quot;</span>
    <span class="n">LogPrefix</span> <span class="o">=</span> <span class="s">&quot;logPrefix&quot;</span>

    <span class="c"># The name of the attribute which will store the created logger</span>
    <span class="n">_Logger</span> <span class="o">=</span> <span class="s">&quot;logger&quot;</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">className</span><span class="p">,</span> <span class="n">parents</span><span class="p">,</span> <span class="n">attributes</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * className -- The name of the class</span>
<span class="sd">        * parents -- The class parents</span>
<span class="sd">        * attributes -- The class attributes</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># Create the logger for this class, and store it in</span>
        <span class="c"># the attributes for the class</span>
        <span class="n">logLevel</span> <span class="o">=</span> <span class="n">attributes</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">LoggerMeta</span><span class="o">.</span><span class="n">LogLevel</span><span class="p">,</span> <span class="n">LogLevel</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
        <span class="n">logPrefix</span> <span class="o">=</span> <span class="n">attributes</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">LoggerMeta</span><span class="o">.</span><span class="n">LogPrefix</span><span class="p">,</span> <span class="n">Prefix</span><span class="p">())</span>

        <span class="n">logger</span> <span class="o">=</span> <span class="n">Logger</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">className</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="n">logPrefix</span><span class="p">,</span> <span class="n">logLevel</span><span class="o">=</span><span class="n">logLevel</span><span class="p">)</span>
        <span class="n">attributes</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">LoggerMeta</span><span class="o">.</span><span class="n">_Logger</span><span class="p">,</span> <span class="n">logger</span><span class="p">)</span>

        <span class="k">return</span> <span class="nb">type</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">className</span><span class="p">,</span> <span class="n">parents</span><span class="p">,</span> <span class="n">attributes</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="FunctionLoggerMeta"><a class="viewcode-back" href="../../../_generated/pyamp_logging_meta.html#pyamp.logging.meta.FunctionLoggerMeta">[docs]</a><span class="k">class</span> <span class="nc">FunctionLoggerMeta</span><span class="p">(</span><span class="n">LoggerMeta</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The FunctionLoggerMeta class is a metaclass that provides the ability</span>
<span class="sd">    to log specific functions of a class. This metaclass uses the</span>
<span class="sd">    :class:`Logger` object (as created by the :class:`LoggerMeta` class) to</span>
<span class="sd">    print debug statements as functions of a class are entered and exited.</span>

<span class="sd">    The following types of functions will NOT be logged:</span>
<span class="sd">        * Builtin functions (i.e., the function name ends with &#39;__&#39;)</span>
<span class="sd">        * Functions that are decorated with the :func:`doNotLog` function</span>

<span class="sd">    All other functions will log a DEBUG message when the function is entered</span>
<span class="sd">    and when the function is exited. This provides an easy way to toggle the</span>
<span class="sd">    ability to view the scope of functions during the runtime of a system.</span>

<span class="sd">    Example::</span>

<span class="sd">        class Example:</span>
<span class="sd">            __metaclass__ = FunctionLoggerMeta</span>

<span class="sd">            def test(self):</span>
<span class="sd">                print &quot;This is a test&quot;</span>


<span class="sd">        if __name__ == &#39;__main__&#39;:</span>
<span class="sd">            example = Example()</span>

<span class="sd">            # Prints:</span>
<span class="sd">            #    [DEBUG]: Entering test</span>
<span class="sd">            #    This is a test</span>
<span class="sd">            #    [DEBUG]: Exiting test</span>
<span class="sd">            example.test()</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="c"># The attribute used to identify functions that should not be logged</span>
    <span class="n">DoNotLog</span> <span class="o">=</span> <span class="s">&quot;doNotLog&quot;</span>

    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">className</span><span class="p">,</span> <span class="n">parents</span><span class="p">,</span> <span class="n">attributes</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * className -- The name of the class</span>
<span class="sd">        * parents -- The class parents</span>
<span class="sd">        * attributes -- The class attributes</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">LoggerMeta</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">className</span><span class="p">,</span> <span class="n">parents</span><span class="p">,</span> <span class="n">attributes</span><span class="p">)</span>

        <span class="c"># Get the logger object created by the LoggerMeta class</span>
        <span class="n">logger</span> <span class="o">=</span> <span class="n">attributes</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">LoggerMeta</span><span class="o">.</span><span class="n">_Logger</span><span class="p">)</span>

        <span class="c"># Traverse through all of the attributes</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">func</span> <span class="ow">in</span> <span class="n">attributes</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="c"># If we should log this function, then wrap it with</span>
            <span class="c"># debug statements which will be printed when the</span>
            <span class="c"># function is entered and exited</span>
            <span class="k">if</span> <span class="n">mcs</span><span class="o">.</span><span class="n">__shouldLog</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
                <span class="k">def</span> <span class="nf">loggingWrapper</span><span class="p">(</span><span class="n">funcName</span><span class="p">,</span> <span class="n">function</span><span class="p">,</span> <span class="n">funcLogger</span><span class="p">):</span>
                    <span class="sd">&#39;&#39;&#39;Function to wrap the decorating function so</span>
<span class="sd">                    that we can pass it other arguments.</span>

<span class="sd">                    * funcName -- The name of the function</span>
<span class="sd">                    * function -- The actual function to call</span>
<span class="sd">                    * funcLogger -- The logger to use for this function</span>

<span class="sd">                    &#39;&#39;&#39;</span>
                    <span class="k">def</span> <span class="nf">tempFunction</span><span class="p">(</span><span class="o">*</span><span class="n">attrs</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                        <span class="sd">&#39;&#39;&#39;Function to decorate the desired function</span>
<span class="sd">                        with debug statements before and after the</span>
<span class="sd">                        function is called.</span>

<span class="sd">                        * attrs -- The incoming attributes</span>
<span class="sd">                        * kwargs -- The incoming keyword arguments</span>

<span class="sd">                        &#39;&#39;&#39;</span>
                        <span class="n">funcLogger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Entering </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">funcName</span><span class="p">)</span>
                        <span class="n">function</span><span class="p">(</span><span class="o">*</span><span class="n">attrs</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
                        <span class="n">funcLogger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;Exiting </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">funcName</span><span class="p">)</span>
                    <span class="k">return</span> <span class="n">tempFunction</span>

                <span class="c"># Overwrite the function with the logging wrapper</span>
                <span class="n">attributes</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">loggingWrapper</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">logger</span><span class="p">)</span>

        <span class="k">return</span> <span class="nb">type</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">className</span><span class="p">,</span> <span class="n">parents</span><span class="p">,</span> <span class="n">attributes</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__shouldLog</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">funcName</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Determine if the given function should be logged.</span>

<span class="sd">        * funcName -- The name of the function</span>
<span class="sd">        * func -- A pointer to the actual function</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="nb">all</span><span class="p">([</span><span class="n">mcs</span><span class="o">.</span><span class="n">__isFunction</span><span class="p">(</span><span class="n">func</span><span class="p">),</span>
                    <span class="n">mcs</span><span class="o">.</span><span class="n">__isNotBuiltin</span><span class="p">(</span><span class="n">funcName</span><span class="p">),</span>
                    <span class="n">mcs</span><span class="o">.</span><span class="n">__canLog</span><span class="p">(</span><span class="n">func</span><span class="p">)])</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__isFunction</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Determine if the given object is a function.</span>

<span class="sd">        * obj -- The object</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s">&#39;__call__&#39;</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__isNotBuiltin</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">objName</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Determine if the given object is a builtin attribute or not.</span>

<span class="sd">        * objName -- The name of the object</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="n">objName</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">&quot;__&quot;</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__canLog</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Determine if the given object can be logged by checking if</span>
<span class="sd">        it has the FunctionLoggerMeta.DoNotLog attribute set.</span>

<span class="sd">        * obj -- The object&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">FunctionLoggerMeta</span><span class="o">.</span><span class="n">DoNotLog</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>

</div>
<span class="k">def</span> <span class="nf">doNotLog</span><span class="p">(</span><span class="n">function</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The doNotLog function is a decorator function which flags a function</span>
<span class="sd">    so that it does not get logged by the :class:`FunctionLoggerMeta` class.</span>

<span class="sd">    Any functions decorated with &#39;doNotLog&#39; will be ignored by the</span>
<span class="sd">    :class:`FunctionLoggerMeta` class.</span>

<span class="sd">    Example::</span>

<span class="sd">        class Example:</span>
<span class="sd">            __metaclass__ = FunctionLoggerMeta</span>

<span class="sd">            # This function will be logged</span>
<span class="sd">            def logged(self):</span>
<span class="sd">                pass</span>

<span class="sd">            # This function will not be logged</span>
<span class="sd">            @doNotLog</span>
<span class="sd">            def notLogged(self):</span>
<span class="sd">                pass</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="nb">setattr</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">FunctionLoggerMeta</span><span class="o">.</span><span class="n">DoNotLog</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">function</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../logging.html" >pyamp.logging</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>